package com.xinyue.game.server.gamehandler;

import com.xinyue.game.messages.LoginRequest;
import com.xinyue.game.messages.LoginResponse;
import com.xinyue.game.server.system.PlayerChannelService;
import com.xinyue.network.hamdlermapping.GameChannelContext;
import com.xinyue.network.hamdlermapping.GameHandlerComponent;
import com.xinyue.network.hamdlermapping.GameMapping;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 处理玩家的登陆功能
 * @author 王广帅
 * @date 2021年01月26日 8:28 下午
 */
@GameHandlerComponent
public class LoginHandler extends AbstractGameHandler {
    //临时定义一个playerId
    private long nowPlayerId= 1000;
    @Autowired
    private PlayerChannelService playerChannelService;
    @GameMapping(LoginRequest.class)
    public void login(GameChannelContext ctx, LoginRequest request) {
        logger.debug("{} 登陆成功", request.getUsername());
        long playerId = ++ nowPlayerId;
        LoginResponse response = new LoginResponse();
        response.setPlayId(playerId);
        //登陆成功之后，将此用户对应的连接加入到连接管理器之中
        playerChannelService.addChannel(playerId,ctx.getChannel());
        ctx.sendGameMessage(response);
    }


}
